const app = require('../app')
const { 
  ACCOUNT_OR_PASSWORD_IS_REQUIRED,
  ACCOUNT_IS_ALREADY_EXISTS,
  ACCOUNT_IS_NOT_EXISTS,
  PASSWORD_IS_INCORRECT,
  UNAUTHORIZATION,
  USER_ID_IS_NULL,
  USER_ID_DOES_NOT_EXIST,
  OPERATION_PERMISSION_EXCEPTION,
  WOKER_IS_ALREADY_EXISTS
} = require('../config/error')

app.on('error', (error, ctx) => {
  let code = 0
  let message = ''

  switch(error) {
    case ACCOUNT_OR_PASSWORD_IS_REQUIRED:
      code = -1001
      message = '账号或密码不能为空 ~'
      break
    case ACCOUNT_IS_ALREADY_EXISTS:
      code = -1002
      message = '账号已存在, 请输入新的账号 ~'
      break
    case ACCOUNT_IS_NOT_EXISTS:
      code = -1003
      message = '账号不存在, 请输入正确的账号 ~'
      break
    case PASSWORD_IS_INCORRECT:
      code = -1004
      message = '密码不正确, 请输入正确的密码 ~'
      break
    case UNAUTHORIZATION:
      code = -1005,
      message = '无效的token或token已过期 ~'
      break
    case USER_ID_IS_NULL:
      code = -1006,
      message = '用户id不能为空 ~'
      break
    case USER_ID_DOES_NOT_EXIST:
      code = -1007,
      message = '用户id不存在 ~'
      break
    case OPERATION_PERMISSION_EXCEPTION:
      code = -1008,
      message = '操作权限不足 ~'
      break
    case WOKER_IS_ALREADY_EXISTS:
      code = -1009,
      message = '师傅已存在 ~'
      break
  }

  ctx.body = {
    code,
    message
  }
})